package com.persistence.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.model.Maquina;
import com.persistence.dao.DAOException;
import com.persistence.dao.DAOManagerMain;
import com.persistence.dao.interfaces.DAOMaquina;

public class MySQLDAOMaquina implements DAOMaquina {
	
	private Maquina maquina = null;
	
	
	public MySQLDAOMaquina() {
	}

	public int insert(Maquina maquina) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("insert into Maquina ("
					+ "Id_Maquina, nombre, disponible "
					+ ") " + "values (?, ?) ");

			stmt.setLong(1, maquina.getNroMaquina());
			stmt.setString(2, maquina.getNombre());
			stmt.setBoolean (3, maquina.isDisponible());
			stmt.executeUpdate();
			
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);

		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int update(Maquina maquina) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("update Maquina " 
							+ "set nombre = ?, disponible=? "
							+ " where id_Personal = ? ");

			stmt.setString(1, maquina.getNombre());
         stmt.setBoolean (2, maquina.isDisponible());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public int eliminate(Maquina maquina) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("delete from Maquina where id_maquina = ? ");
			stmt.setLong(1, maquina.getNroMaquina());

			stmt.executeUpdate();
			return 1;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public ArrayList<Maquina> select() throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<Maquina> maquinas = new ArrayList<Maquina>();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection.prepareStatement("select * from Maquina");
			rs = stmt.executeQuery();

			while (rs.next()) {
				Maquina maquina = new Maquina();
				maquina.setNroMaquina(rs.getInt("Id_Maquina"));
				maquina.setNombre(rs.getString("nombre"));
				maquina.setDisponible(rs.getBoolean("disponible"));

				maquinas.add(maquina);
			}

			return maquinas;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	public Maquina findById(int id) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		maquina = new Maquina();

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Maquina where Id_Maquina = ?");
			stmt.setLong(1, id);
			rs = stmt.executeQuery();

			if (rs.next()) {
			   maquina.setNroMaquina(rs.getInt("Id_Maquina"));
            maquina.setNombre(rs.getString("nombre"));
            maquina.setDisponible(rs.getBoolean("disponible"));
			}

			return maquina;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}

	@Override
	public boolean existInstance(Maquina maquina) throws DAOException {
		Connection connection = null;
		ResultSet rs = null;
		PreparedStatement stmt = null;
		boolean result = false;

		try {
			connection = DAOManagerMain.manager().getConnection();
			stmt = connection
					.prepareStatement("select * from Maquina where UCASE(nombre)");

			stmt.setString(1, maquina.getNombre().toUpperCase());
//			stmt.setString(2, maquina.getApellido().toUpperCase());
			rs = stmt.executeQuery();

			if (rs.next()) {
				result = true;
			}

			return result;
		} catch (SQLException sqle) {
			throw new DAOException(sqle);
		} finally {
			DAOManagerMain.manager().closeAll(connection, stmt, rs);
		}
	}
}
